<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="stylesheet" type="text/css" href="resources/tablet.css">
<link rel="stylesheet" type="text/css" href="resources/transition.css">
<script src="resources/perf_test_helper.js"></script>
<style>
target {
  opacity: 0;
}
</style>

<container id="container"></container>

<script>
var N = PerfTestHelper.getN(500);
var duration = 1000;
var keyframeValues = [0, 1];
var id = 0;

for (var i = 0; i < N; i++) {
  var target = document.createElement('target');
  target._keyframe = 0;
  target.style.transitionDelay = (-i / N * duration) + 'ms';
  container.appendChild(target);
  target.addEventListener('transitionend', restartTransition);
}

function restartTransition(event) {
  event.target._keyframe ^= 1;
  addNewIDStyle(event.target);
  event.target.style.removeProperty('transition-delay');
}

function addNewIDStyle(target) {
  id++;
  if (target._currentIDStyle) {
    target._currentIDStyle.remove();
  }
  var style = document.createElement('style');
  style.textContent = '#id' + id + ' { opacity: ' + keyframeValues[target._keyframe] + ';}';
  container.appendChild(style);
  target.id = 'id' + id;
  target._currentIDStyle = style;
}

requestAnimationFrame(function() {
  var styleRule = document.styleSheets[2].rules[0].style;
  styleRule.opacity = keyframeValues[1];
  requestAnimationFrame(function() {
    styleRule.opacity = keyframeValues[0];
  });
});

PerfTestHelper.signalReady();
</script>
